import smtplib
# 为了发送附件，需要使用MIME相关的类
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# 创建一个可以包含附件的邮件，这种邮件包含文字，附件等多个部分，所以被称为MultiPart
msg = MIMEMultipart()
msg['subject'] = "欢迎你，张三"
msg['from'] = "3022696063@qq.com"
msg['to'] = 'zjueman@qq.com'
# 在邮件中添加文字，使用MIMEText表示多个部分中的文字部分
msg.attach(MIMEText('This is test email'))

# 要发送的附件，请把附件放在程序同目录下
filename = '张三.docx'
with open(filename, 'rb') as f:
    # 从文件中读取内容，放置到MIMEApplication中
    part = MIMEApplication(f.read())
    # 设置附件的文件名称
    part.add_header('Content-Disposition', 'attachment', filename=filename)
    # 把附件添加到邮件中
    msg.attach(part)

    server = smtplib.SMTP(host='smtp.qq.com', port=587)
    server.login("3022696063@qq.com", 'onktkaneabxtdfbc')
    server.send_message(msg)

    server.quit()

